package com.sdhs.initdata.service.impl;

import com.sdhs.common.core.domain.entity.SysUser;
import com.sdhs.initdata.domain.SfPerson;
import com.sdhs.initdata.mapper.InitDataMapper;
import com.sdhs.initdata.service.IInitDataService;
import com.sdhs.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class InitDataServiceImpl implements IInitDataService {

    @Autowired
    private InitDataMapper initDataMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean initSfPerson() {
        // 获取所有的用户
        List<SysUser> suserList = initDataMapper.selectAllUser();

        // 遍历用户

        List<SysUser> personList = initDataMapper.selectAllSfPerson();
        List<String> userIdList = new ArrayList<>();
        personList.forEach(person -> {
            if(person != null && person.getUserId() != null){
                userIdList.add(person.getUserId());
            }
        });

        suserList.forEach(suser -> {
            SfPerson sfPerson = new SfPerson();
            if(suser.getAccountType() == null || !"work".equals(suser.getAccountType())) {
                sfPerson.setDelFlag(Long.valueOf(suser.getDelFlag()));
                sfPerson.setType("2");
                sfPerson.setName(suser.getNickName());
                sfPerson.setSex(suser.getSex());
                sfPerson.setIdNumber(suser.getIdCard());
                sfPerson.setLinkNumber(suser.getPhonenumber());
                sfPerson.setDeptId(suser.getDeptId());
                sfPerson.setIsUser("Y");
                sfPerson.setUserId(suser.getUserId());
                sfPerson.setUserName(suser.getUserName());

                if (userIdList.contains(suser.getUserId())) {
                    initDataMapper.updateSfPersonByUserId(sfPerson);
                } else {
                    initDataMapper.insertSfPerson(sfPerson);
                }
            }
        });

        return true;
    }
}
